package com.tian.mq;

import com.tian.message.PhoneMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年05月14日 22:21
 * 博客地址：<a href="http://woaijava.cc/">博客地址</a>
 * <p>
 * 支付订单延迟消息队列
 */
@Component
@Scope(scopeName = "prototype")
public class PhoneMessageProducer {
    private final RabbitTemplate rabbitTemplate;

    @Value("${rabbitmq.phon.message.exchange.name}")
    private String phoneMessageExchangeName;
    @Value("${rabbitmq.phon.message.binding.key}")
    private String phoneMessageBindingKey;

    public PhoneMessageProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 订单支付倒计时
     */
    public void sendPhoneMessage(String phone, String content) {
        // 生成消息的唯一id
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        PhoneMessage phoneMessage = new PhoneMessage();
        phoneMessage.setPhone(phone);
        phoneMessage.setContent(content);
        phoneMessage.setCreateTime(createTime);
        // 发送消息
        rabbitTemplate.convertAndSend(phoneMessageExchangeName,phoneMessageBindingKey,phoneMessage);
    }
}


